﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Data.SqlClient;
using Entidades.Karaoke;

namespace Datos.Karaoke
{
    public class KarBoxDAL
    {
        public static string cnx = ConfigurationManager.ConnectionStrings["ApplicationServices"].ToString();

        public static List<KarBox> ListarBox()
        {
            SqlConnection con = new SqlConnection(cnx);
            SqlCommand com = new SqlCommand("SELECT [NroBox], [Estado], [Capacidad] FROM [Kar_Box] " , con);
            com.CommandType = System.Data.CommandType.Text;
            try
            {
                con.Open();
                SqlDataReader dr = com.ExecuteReader();
                List<KarBox> lista = new List<KarBox>();
                KarBox obj = null;
                while (dr.Read())
                {
                    obj = new KarBox();

                    obj.NroBox = Convert.ToInt32(dr["NroBox"].ToString());
                    obj.Capacidad = Convert.ToInt32(dr["Capacidad"].ToString());
                    obj.Estado = ( Convert.ToBoolean(dr["Estado"])?1:0);
                    obj.strEstado = (obj.Estado == 0 ? "Libre" : "Activado");

                    lista.Add(obj);
                }
                dr.Close();

                return lista;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                con.Close();
            }
        }

        public static KarBox ObtenerBox(int NroBox)
        {
            SqlConnection con = new SqlConnection(cnx);
            SqlCommand com = new SqlCommand("SELECT [NroBox], [Estado], [Capacidad] FROM [Kar_Box] where [NroBox]="+NroBox, con);
            
            com.CommandType = System.Data.CommandType.Text;
            try
            {
                con.Open();
                SqlDataReader dr = com.ExecuteReader();
                KarBox obj = null;
                while (dr.Read())
                {
                    obj = new KarBox();

                    obj.NroBox = Convert.ToInt32(dr["NroBox"].ToString());
                    obj.Capacidad = Convert.ToInt32(dr["Capacidad"].ToString());
                    obj.Estado = Convert.ToInt32(dr["Estado"].ToString());

                }
                dr.Close();

                return obj;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                con.Close();
            }
        }



        public static int actualizar(int nro,int tipo, Boolean estado)
        {
            SqlConnection con = new SqlConnection(cnx);
            SqlCommand com = new SqlCommand("update  [Kar_"+(tipo==2?"Mesa":"Box")+"] set estado="+(estado?1:0)+" where [Nro"+(tipo==2?"Mesa":"Box")+"]="+ nro, con);

            com.CommandType = System.Data.CommandType.Text;
            try
            {
                con.Open();
                Int32 respuesta = com.ExecuteNonQuery();

                return respuesta;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                con.Close();
            }
        }
    }
}
